package com.scott;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpClientDemo {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                whileRun();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                whileRun2();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                whileRun3();
            }
        }).start();
    }

    public static void whileRun() {
        while (true) {
            String res = doPost("http://192.168.1.211/hello", "");
            System.out.println(res);

            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void whileRun2() {
        while (true) {
            String res = doPost("http://192.168.1.211/hello2", "");
            System.out.println(res);

            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void whileRun3() {
        while (true) {
            String res = doPost("http://192.168.1.211/hello3", "");
            System.out.println(res);

            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    public static String doPost(String url, String param) {
        String responseBody = null;
        CloseableHttpClient httpclient = null;
        try {
            httpclient = HttpClients.createDefault();
            HttpGet httpPost = new HttpGet(url);
            httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");

            ResponseHandler responseHandler = new ResponseHandler() {
                public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
                    int status = response.getStatusLine().getStatusCode();
                    if ((status >= 200) && (status < 300)) {
                        HttpEntity entity = response.getEntity();

                        return entity != null ? EntityUtils.toString(entity) : null;
                    }
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            };
            responseBody = (String) httpclient.execute(httpPost, responseHandler);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
            }
        }
        return responseBody;
    }
}
